package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.system.domain.SysGateway;
import com.ruoyi.system.domain.cache.DeviceCache;
import com.ruoyi.system.domain.cache.GatewayCache;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysGatewayCycle;
import com.ruoyi.system.service.ISysGatewayCycleService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 网关采集周期Controller
 * 
 * @author ruoyi
 * @date 2024-09-30
 */
@RestController
@RequestMapping("/system/gateway")
public class SysGatewayCycleController extends BaseController
{
    @Autowired
    private ISysGatewayCycleService sysGatewayCycleService;


    @Autowired
    private RedisCache redisCache;
    /**
     * 查询网关列表
     */
    @PreAuthorize("@ss.hasPermi('gateway:cycle:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysGateway sysGateway)
    {
        startPage();
        if (!getDeptId().equals(100L)){
            sysGateway.setDeptId(getDeptId());
        }
        List<SysGateway> list = sysGatewayCycleService.selectSysGatewayList(sysGateway);
        list.forEach(item -> {
            GatewayCache gatewayCache = redisCache.getCacheObject(CacheConstants.IOT_GATEWAY + item.getGatewayId());
            if (gatewayCache != null){
                item.setRssi(gatewayCache.getRssi());
                item.setBatteryVoltage(gatewayCache.getBatteryVoltage());
                item.setTimeSample(gatewayCache.getTimeSample());
                item.setTimeUpload(gatewayCache.getTimeUpload());
            }
        });
        return getDataTable(list);
    }

    /**
     * 导出网关采集周期列表
     */
    @PreAuthorize("@ss.hasPermi('gateway:cycle:export')")
    @Log(title = "网关采集周期", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysGatewayCycle sysGatewayCycle)
    {
        List<SysGatewayCycle> list = sysGatewayCycleService.selectSysGatewayCycleList(sysGatewayCycle);
        ExcelUtil<SysGatewayCycle> util = new ExcelUtil<SysGatewayCycle>(SysGatewayCycle.class);
        util.exportExcel(response, list, "网关采集周期数据");
    }

    /**
     * 获取网关采集周期详细信息
     */
    @PreAuthorize("@ss.hasPermi('gateway:cycle:query')")
    @GetMapping(value = "/{gatewayId}")
    public AjaxResult getInfo(@PathVariable("gatewayId") Long gatewayId)
    {
        return success(sysGatewayCycleService.selectSysGatewayCycleByGatewayId(gatewayId));
    }

    /**
     * 新增网关采集周期
     */
    @PreAuthorize("@ss.hasPermi('gateway:cycle:add')")
    @Log(title = "网关采集周期", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysGatewayCycle sysGatewayCycle)
    {
        return toAjax(sysGatewayCycleService.insertSysGatewayCycle(sysGatewayCycle));
    }

    /**
     * 修改网关采集周期
     */
    @PreAuthorize("@ss.hasPermi('gateway:cycle:edit')")
    @Log(title = "网关采集周期", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysGatewayCycle sysGatewayCycle)
    {
        return toAjax(sysGatewayCycleService.updateSysGatewayCycle(sysGatewayCycle));
    }

    /**
     * 删除网关采集周期
     */
    @PreAuthorize("@ss.hasPermi('gateway:cycle:remove')")
    @Log(title = "网关采集周期", businessType = BusinessType.DELETE)
	@DeleteMapping("/{gatewayIds}")
    public AjaxResult remove(@PathVariable Long[] gatewayIds)
    {
        return toAjax(sysGatewayCycleService.deleteSysGatewayCycleByGatewayIds(gatewayIds));
    }



}
